package com.magic.gre.behavior;

import android.view.View;
import android.widget.Scroller;

import com.noname.lib_base_java.util.L;

/**
 * Author: Lmoumou
 * Date: 2019-01-14 15:55
 * 文件名: ScrollerRunnable
 * 描述:
 */
public class ScrollerRunnable implements Runnable {

    private Scroller scroller;
    private View childView;
    private int height;

    public ScrollerRunnable(Scroller scroller, View childView, int height) {
        this.scroller = scroller;
        this.childView = childView;
        this.height = height;
    }


    public void scrollToOpen() {
        int scrollY = (int) childView.getTranslationY();
        scroller.startScroll(0, scrollY, 0, -scrollY);
        startScroll();
    }

    public void scrollToClose() {
        int currY = (int) childView.getTranslationY();
        int scrollY = height - Math.abs(currY);
        scroller.startScroll(0, currY, 0, -scrollY);
        startScroll();
    }

    private void startScroll() {
        if (scroller.computeScrollOffset()) {
            childView.postDelayed(this, 16);
        }
    }

    @Override
    public void run() {
        if (scroller.computeScrollOffset()) {
            childView.setTranslationY(scroller.getCurrY());
            childView.postDelayed(this, 16);
        }
    }
}
